package cn.yunhe.servlet;

import cn.yunhe.bean.Area;
import cn.yunhe.bean.SceType;
import cn.yunhe.bean.Scenery;
import cn.yunhe.biz.SceneryService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/AddSceneryServlet")
public class AddSceneryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DiskFileItemFactory diskFileItemFactory = null;
        ServletFileUpload servletFileUpload = null;
        if(servletFileUpload.isMultipartContent(request)){
            try {
                diskFileItemFactory = new DiskFileItemFactory();
                servletFileUpload = new ServletFileUpload(diskFileItemFactory);
                servletFileUpload.setSizeMax(10*1024*1024);
                servletFileUpload.setHeaderEncoding("UTF_8");
                List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
                if(fileItemList!=null){
                    Scenery scenery = new Scenery();
                    for (FileItem fileItem:fileItemList){
                        if(fileItem.isFormField()){
                            switch (fileItem.getFieldName()){
                                case "scename":
                                    String scename = fileItem.getString("utf-8");
                                    scenery.setSceName(scename);
                                    break;
                                case "descript":
                                    String descript = fileItem.getString("utf-8");
                                    scenery.setContext(descript);
                                    break;
                                case "sceType":
                                    String sceTypeId = fileItem.getString("utf-8");
                                    SceType sceType = new SceType();
                                    sceType.setSceTypeId(Integer.parseInt(sceTypeId));
                                    scenery.setScetype(sceType);
                                    break;
                                case "sceArea":
                                    String areaId = fileItem.getString("utf-8");
                                    Area area = new Area();
                                    area.setAreaId(Integer.parseInt(areaId));
                                    scenery.setArea(area);
                                    break;
                                case "ticketPrice":
                                    String ticketPrice = fileItem.getString("utf-8");
                                    scenery.setTicket(Integer.parseInt(ticketPrice));
                                    break;
                                case "ticketCount":
                                    String ticketCount = fileItem.getString("utf-8");
                                    scenery.setTicCount(Integer.parseInt(ticketCount));
                                    break;
                                case "adress":
                                    String adress = fileItem.getString("utf-8");
                                    scenery.setLocation(adress);
                                    break;
                                case "season":
                                    String season = fileItem.getString("utf-8");
                                    scenery.setSeason(season);
                                    break;
                                case "startTime":
                                    String startTime = fileItem.getString("utf-8");
                                    scenery.setStartTime(startTime);
                                    break;
                                case "sceGrade":
                                    String sceGrade = fileItem.getString("utf-8");
                                    scenery.setSceGrade(Integer.parseInt(sceGrade));
                                    break;
                            }
                        }else{
                            String fileName = fileItem.getName();
                            String realpath = request.getSession().getServletContext().getRealPath("/img");
                            int index = realpath.lastIndexOf("img");
                            String path = realpath.substring(index)+"/"+fileName;
                            scenery.setPic(path);
                            File file = new File(realpath,fileName);
                           fileItem.write(file);
                        }
                    }
                    SceneryService sceneryService = new SceneryService();
                    int result = sceneryService.addScenery(scenery);
                    PrintWriter out = response.getWriter();
                    out.print("<script>alert('添加成功');location.href='adminPage.jsp';</script>");
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
